/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package cn.piaoyi.comm;

import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
public class ImgUtil {

	private static final Log logger = LogFactory.getLog(ImgUtil.class);

	public ImgUtil() {
	}
	/***功能：图片加logo图片水印***
	  *参数1：oImage：原图；*
	  *参数2：newImageName：加logo图片水印存放的路径和新名字；*
	  *参数3：fileType：加logo图片水印图片类型（png,gif,jpg...）；
	  *参数4：logoPath：logo水印图片的存放路径****/
	  public static boolean pressImage(File oImage, String newImageName, String fileType, String logoPath) throws IOException{
	        File waterMarkImage = new File(logoPath);
	        if (!waterMarkImage.exists()) {
	         return false;
	        }
	        BufferedImage originalImage = ImageIO.read(oImage);
	        BufferedImage watermarkImage = ImageIO.read(waterMarkImage);
	        int originalWidth = originalImage.getWidth(null);
	        int originalHeight = originalImage.getHeight(null);
	        int watermarkWidth = watermarkImage.getWidth(null);
	        int watermarkHeight = watermarkImage.getHeight(null);
	        if (originalWidth <= watermarkWidth || originalHeight <= watermarkHeight || originalWidth < 50 || originalHeight < 50) {
	         return false;
	        }
	        BufferedImage newImage = new BufferedImage(originalWidth, originalHeight, BufferedImage.TYPE_INT_RGB);
	        Graphics2D g = newImage.createGraphics();
	        g.drawImage(originalImage, 0, 0, originalWidth, originalHeight, null);
	        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
	        g.drawImage(watermarkImage, originalWidth-watermarkWidth-10, originalHeight-watermarkHeight-10, watermarkWidth, watermarkHeight, null);
	        g.dispose();
	        ImageIO.write(newImage, fileType, new FileOutputStream(newImageName));// 存盘
	        return true;
	    }
}
